package stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * stream API/流式计算
 */
public class TestStream2 {

    public static void main(String[] args) {

        List<Stu> stus = new ArrayList<>();
        stus.add(new Stu(100, "jack", "bj", "篮球"));
        stus.add(new Stu(200, "marry", "sh", "足球"));
        stus.add(new Stu(300, "smith", "gz", "羽毛球"));
        stus.add(new Stu(400, "milan", "sz", "乒乓球"));

        //需求：将stus -> map k-string v-List<Stu2>
        //Stu(100, "jack", "bj", "篮球") -> 100 -> 两个Stu2【("jack", "篮球"), ("jack-克隆", "篮球")】
        //可以使用流式计算的 将list 转成 map：Collectors.toMap()
        //stu就是遍历stus的每个Stu对象
        Map<String, List<Stu2>> map = stus.stream().collect(Collectors.toMap(stu -> {
            return stu.getId().toString();
        }, stu -> {
            List<Stu2> stu2List = new ArrayList<>();
            stu2List.add(new Stu2(stu.getName(), stu.getHobby()));
            stu2List.add(new Stu2(stu.getName() + "-克隆", stu.getHobby()));
            return stu2List;
        }));

        System.out.println("map->" + map);
    }

}

class Stu {

    private Integer id;

    private String name;

    private String address;

    private String hobby;

    public Stu(Integer id, String name, String address, String hobby) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.hobby = hobby;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", hobby='" + hobby + '\'' +
                '}';
    }
}

class Stu2 {

    private String name;

    private String hobby;

    public Stu2(String name, String hobby) {
        this.name = name;
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "Stu2{" +
                "name='" + name + '\'' +
                ", hobby='" + hobby + '\'' +
                '}';
    }
}
